using System;
using System.Collections.Generic;
using System.Text;
using DotNetMatrix;
using ScannerLib.Vector;
using System.IO;

namespace ScannerLib.Tsai
{
    public class CameraCalibrationData
    {
        private GeneralMatrix m_rotationMatrix;
        private BasicVector m_translationVector;
        private double m_kappa, m_f, m_ncx, m_nfx, m_dx, m_dy, m_dpx, m_dpy, m_cx, m_cy, m_sx, m_p1, m_p2;
        private string m_calibrationDataFile;
        private BasicVector m_lightCalibrationData = new BasicVector(0, 0, 0);

        public double P2
        {
            get { return m_p2; }
          
        }

        public double P1
        {
            get { return m_p1; }
         
        }

        public double Sx
        {
            get { return m_sx; }
          
        }

        public double Cy
        {
            get { return m_cy; }
           
        }

        public double Cx
        {
            get { return m_cx; }
           
        }

        public double Dpy
        {
            get { return m_dpy; }
         
        }

        public double Dpx
        {
            get { return m_dpx; }
         
        }

        public double Dy
        {
            get { return m_dy; }
          
        }

        public double Dx
        {
            get { return m_dx; }
           
        }

        public double Nfx
        {
            get { return m_nfx; }
          
        }

        public double Ncx
        {
            get { return m_ncx; }
           
        }

        public double F
        {
            get { return m_f; }
            
        }

        public double Kappa
        {
            get { return m_kappa; }
            
        }

        public string OriginalDataFile
        {
            get
            {
                return m_calibrationDataFile;
            }
        }

        public BasicVector LightCalibrationData
        {
            get
            {
                return m_lightCalibrationData;
            }

            set
            {
                m_lightCalibrationData = value;
            }
        }

        public CameraCalibrationData(StringReader sr)
        {

            m_calibrationDataFile = sr.ReadToEnd();
            sr = new StringReader(m_calibrationDataFile.ToString());

            m_ncx = Convert.ToDouble(sr.ReadLine().Trim());
            m_nfx = Convert.ToDouble(sr.ReadLine().Trim());
            m_dx = Convert.ToDouble(sr.ReadLine().Trim());
            m_dy = Convert.ToDouble(sr.ReadLine().Trim());

            m_dpx = Convert.ToDouble(sr.ReadLine().Trim());
            m_dpy = Convert.ToDouble(sr.ReadLine().Trim());

            m_cx = Convert.ToDouble(sr.ReadLine().Trim());
            m_cy = Convert.ToDouble(sr.ReadLine().Trim());

            m_sx = Convert.ToDouble(sr.ReadLine().Trim());

            m_f = Convert.ToDouble(sr.ReadLine().Trim());
            m_kappa = Convert.ToDouble(sr.ReadLine().Trim());

            double tx, ty, tz, rx, ry, rz, sa,ca,sb,cb,sg,cg;

            tx = Convert.ToDouble(sr.ReadLine().Trim());
            ty = Convert.ToDouble(sr.ReadLine().Trim());
            tz = Convert.ToDouble(sr.ReadLine().Trim());

            rx = Convert.ToDouble(sr.ReadLine().Trim());
            ry = Convert.ToDouble(sr.ReadLine().Trim());
            rz = Convert.ToDouble(sr.ReadLine().Trim());

            m_translationVector = new BasicVector(tx, ty, tz);

            m_rotationMatrix = new GeneralMatrix(3, 3);


            SINCOS(rx, out sa, out ca);
            SINCOS(ry, out sb, out cb);
            SINCOS(rz, out sg, out cg);

            m_rotationMatrix.SetElement(0,0,cb * cg);
            m_rotationMatrix.SetElement(1, 0, cg * sa * sb - ca * sg);
            m_rotationMatrix.SetElement(2, 0, sa * sg + ca * cg * sb);
            m_rotationMatrix.SetElement(0, 1, cb * sg);
            m_rotationMatrix.SetElement(1, 1, sa * sb * sg + ca * cg);
            m_rotationMatrix.SetElement(2, 1, ca * sb * sg - cg * sa);
            m_rotationMatrix.SetElement(0, 2, -sb);
            m_rotationMatrix.SetElement(1,2, cb * sa);
            m_rotationMatrix.SetElement(2,2,ca * cb);

            m_p1 = Convert.ToDouble(sr.ReadLine().Trim());
            m_p2 = Convert.ToDouble(sr.ReadLine().Trim());


            //m_rotationMatrix.SetElement(0,0, 
           
        }

        
        private void SINCOS(double rz, out double sg, out double cg)
        {
            sg = Math.Sin(rz);
            cg = Math.Cos(rz);
        }

    }
}
